package com.example.tangjiangxu.servicetest;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Context;
import android.content.Intent;
import android.graphics.BitmapFactory;
import android.graphics.Color;
import android.net.Uri;
import android.os.Binder;
import android.os.Build;
import android.os.IBinder;
import android.support.annotation.Nullable;
import android.support.v4.app.NotificationCompat;
import android.util.Log;

/**
 * Created by tangjiangxu on 3/1/17.
 */

public class MyService extends Service {
    private static final String TAG = "MyService";

    private MyBinder mBinder = new MyBinder();

    /**
     * 只会在Service与Activity第一次建立关联时，如果Activity与Service绑定成功，
     * 后面不管调用多少次bindService, 都不会调用onBind方法
     * @param intent
     * @return
     */
    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        Log.e(TAG, "onBind: ");
        return mBinder;
    }

    /**
     * 只会在Service第一次被创建的时候调用，如果当前Service已经被创建过了，
     * 不管怎样调用startService()／bindService方法，onCreate()方法都不会再执行
     */
    @Override
    public void onCreate() {
        Log.e(TAG, "onCreate: ");
        Log.e(TAG, "onCreate: Activity thread id is: " + Thread.currentThread().getId());

        NotificationManager ntfM = (NotificationManager) this.getSystemService(Context.NOTIFICATION_SERVICE);
        Notification ntf = null;
        Intent intentNotification = new Intent(this, MainActivity.class);
        intentNotification.addFlags(Intent.FLAG_ACTIVITY_CLEAR_TOP | Intent.FLAG_ACTIVITY_NEW_TASK);
        intentNotification.setData(Uri.parse("custom://" + System.currentTimeMillis()));
        PendingIntent pendingIntent = PendingIntent.getActivity(this, 0 ,intentNotification, 0);
        NotificationCompat.Builder builder = new NotificationCompat.Builder(this);
        if (Build.VERSION.SDK_INT >= 21) {
            builder.setSmallIcon(R.mipmap.ic_launcher);
            builder.setColor(Color.parseColor("#FFE94424"));
        } else {
            builder.setSmallIcon(R.mipmap.ic_launcher);
        }
        builder.setLargeIcon(BitmapFactory.decodeResource(this.getResources(), R.mipmap.ic_launcher))
                .setTicker("Receive a message from That's")
                .setWhen(System.currentTimeMillis())
                .setContentTitle("这个是标题")
                .setContentText("这个是内容")
                .setContentIntent(pendingIntent);
        ntf = builder.getNotification();

        ntf.flags = Notification.FLAG_AUTO_CANCEL;
        ntfM.notify(0, ntf);
        //startForeground(1, ntf);
        super.onCreate();
    }

    /**
     * 每次startService都会调用
     * @param intent
     * @param flags
     * @param startId
     * @return
     */
    @Override
    public int onStartCommand(Intent intent, int flags, int startId) {
        Log.e(TAG, "onStartCommand: ");
        return super.onStartCommand(intent, flags, startId);
    }

    /**
     * 只会在stopService／unbindService时，调用一次
     * 只有当Service已停止了，且没有与任何Activity关联时，才会被调用
     * 所以，当Service 被start并且bind时，需要调用stop和unbind才能触发onDestory方法
     * 该方法清理掉那些不再使用的资源，防止Service在销毁后，还会有一些不再使用的资源存在
     */
    @Override
    public void onDestroy() {
        super.onDestroy();
        Log.e(TAG, "onDestroy: ");

    }

    /**
     * 自定的Binder
     */
    class MyBinder extends Binder {

        public void startDownload() {
            Log.e(TAG, "startDownload: " );
        }
    }
}
